package com.sevencar.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lightble.BlueToothService;
import com.example.lightble.R;


public class MainActivity extends FragmentActivity implements OnPageChangeListener{

	protected static final String TAG = "MainActivity";

	private TextView title_tv;
	private Button[] mTabs;
	private OrderManageFragment orderManageFragment;//����ҳ��
	private CommentManageFragment commentManageFragment;//���۹������
	private MineFragment mineFragment;//�ҵĽ���
	private HomeFragment homeFragment;//��
	private List<Fragment> fragments = new ArrayList<Fragment>();
	private int index;
	// ��ǰfragment��index
	private int currentTabIndex;
	
	private ViewPager mViewPager;
	
	//--------------------------ble
	private int REQUEST_ENABLE_BT=1;
	
	Intent mBlueToothServiceIntent;
	
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();

		homeFragment = new HomeFragment();
		commentManageFragment = new CommentManageFragment();
		orderManageFragment = new OrderManageFragment();
		mineFragment = new MineFragment();
		
		fragments.add(homeFragment);
		fragments.add(commentManageFragment);
		fragments.add(orderManageFragment);
		fragments.add(mineFragment);
		
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		MyPagerAdapter mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(this);
		
		
		if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
		    Toast.makeText(this, "抱歉 该手机不支持蓝牙4.0", Toast.LENGTH_SHORT).show();
		}else{
			final BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
			BluetoothAdapter  mBluetoothAdapter = bluetoothManager.getAdapter();
			
			if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
				Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
			}
			
			mBlueToothServiceIntent=new Intent(this,BlueToothService.class);
			startService(mBlueToothServiceIntent);	
			
		}
		
		
		

	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopService(mBlueToothServiceIntent);
	}

	private void initView() {
		title_tv = (TextView) findViewById(R.id.title_tv);//
		mTabs = new Button[4];
		mTabs[0] = (Button) findViewById(R.id.btn_conversation_zaina);//���İ�ť
		mTabs[1] = (Button) findViewById(R.id.btn_gushi);//���°�ť
		mTabs[2] = (Button) findViewById(R.id.btn_conversation);//���лỰ��ť
		mTabs[3] = (Button) findViewById(R.id.btn_address_list);//��ϵ�˰�ť
		mTabs[0].setSelected(true);

	}

	public void onTabClicked(View view) {
		switch (view.getId()) {
		case R.id.btn_conversation_zaina:
			index = 0;
			break;
		case R.id.btn_gushi:
			index = 1;
			break;
		case R.id.btn_conversation:
			index = 2;
			break;
		case R.id.btn_address_list:
			index = 3;
			break;
		}
		mViewPager.setCurrentItem(index, false);
		
		mTabs[currentTabIndex].setSelected(false);
		mTabs[index].setSelected(true);
		currentTabIndex = index;
		_setTitle(index);
		
		
	}
	 private long exitTime = 0;

	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	        if (keyCode == KeyEvent.KEYCODE_BACK
	                && event.getAction() == KeyEvent.ACTION_DOWN) {
	            if ((System.currentTimeMillis() - exitTime) > 2000) {
	                Toast.makeText(getApplicationContext(), "再按一次退出",
	                        Toast.LENGTH_SHORT).show();
	                exitTime = System.currentTimeMillis();
	            } else {
	            	stopService(mBlueToothServiceIntent);
	                finish();
	                System.exit(0);
	            }
	            return true;
	        }
	        return super.onKeyDown(keyCode, event);
	 }	
	 
	 private class MyPagerAdapter extends FragmentPagerAdapter  {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return fragments.get(arg0);
		}

		@Override
		public int getCount() {
			return 4;
		}
		
		
	 }

	@Override
	public void onPageScrollStateChanged(int arg0) {
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
	}

	@Override
	public void onPageSelected(int arg0) {
		index = arg0;
		mTabs[currentTabIndex].setSelected(false);
		mTabs[index].setSelected(true);
		initdrawaletop();
		if(arg0 == 0){
			Drawable drawableTop = getResources().getDrawable(R.drawable.ss1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(arg0 == 1){
			Drawable drawableTop = getResources().getDrawable(R.drawable.ss2);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(arg0 == 2){
			Drawable drawableTop = getResources().getDrawable(R.drawable.ss3);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(arg0 == 3){
			Drawable drawableTop = getResources().getDrawable(R.drawable.ss4);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}
		currentTabIndex = index;
		_setTitle(arg0);
	}
	private void initdrawaletop(){
		
		Drawable drawableTop0 = getResources().getDrawable(R.drawable.sss1);
		mTabs[0].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop0 , null, null);
		
		Drawable drawableTop1 = getResources().getDrawable(R.drawable.sss2);
		mTabs[1].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop1 , null, null);
		
		Drawable drawableTop2 = getResources().getDrawable(R.drawable.sss3);
		mTabs[2].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop2 , null, null);
		
		Drawable drawableTop3 = getResources().getDrawable(R.drawable.sss4);
		mTabs[3].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop3 , null, null);
		
	}
	private void _setTitle(int i){
		if(title_tv==null){
			return;
		}
		if(i==0){
			title_tv.setText("首页");
		}else if(i==1){
			title_tv.setText("彩灯");
		}else if(i==2){
			title_tv.setText("情景");
		}else if(i==3){
			title_tv.setText("设置");
		}else{
			title_tv.setText("首页");
			
		}
	}
		
	
	
}